<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>MySQL 驱动 Add Batch 优化实现 | flyingzc's blog</title><meta name="author" content="flyingzc"><meta name="copyright" content="flyingzc"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="MySQL 驱动 Add Batch 优化实现MySQL 驱动会在 JDBC URL 添加 rewriteBatchedStatements 参数时，对 batch 操作进行优化。本文测试各种参数组合的行为，并结合驱动代码简单分析。 batch参数组合行为useServerPrepStmts 参数123PreparedStatement psmt &#x3D; connection.prepareState">
<meta property="og:type" content="article">
<meta property="og:title" content="MySQL 驱动 Add Batch 优化实现">
<meta property="og:url" content="https://flyingzc.github.io/articles/mysql/MySQL%E9%A9%B1%E5%8A%A8add-batch%E4%BC%98%E5%8C%96%E5%AE%9E%E7%8E%B0.html">
<meta property="og:site_name" content="flyingzc&#39;s blog">
<meta property="og:description" content="MySQL 驱动 Add Batch 优化实现MySQL 驱动会在 JDBC URL 添加 rewriteBatchedStatements 参数时，对 batch 操作进行优化。本文测试各种参数组合的行为，并结合驱动代码简单分析。 batch参数组合行为useServerPrepStmts 参数123PreparedStatement psmt &#x3D; connection.prepareState">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://flyingzc.github.io/images/zc-icon.jpeg">
<meta property="article:published_time" content="2024-03-25T16:00:00.000Z">
<meta property="article:modified_time" content="2025-01-21T01:51:41.212Z">
<meta property="article:author" content="flyingzc">
<meta property="article:tag" content="MySQL">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://flyingzc.github.io/images/zc-icon.jpeg"><script type="application/ld+json">{
  "@context": "https://schema.org",
  "@type": "BlogPosting",
  "headline": "MySQL 驱动 Add Batch 优化实现",
  "url": "https://flyingzc.github.io/articles/mysql/MySQL%E9%A9%B1%E5%8A%A8add-batch%E4%BC%98%E5%8C%96%E5%AE%9E%E7%8E%B0.html",
  "image": "https://flyingzc.github.io/images/zc-icon.jpeg",
  "datePublished": "2024-03-25T16:00:00.000Z",
  "dateModified": "2025-01-21T01:51:41.212Z",
  "author": [
    {
      "@type": "Person",
      "name": "flyingzc",
      "url": "https://flyingzc.github.io/"
    }
  ]
}</script><link rel="shortcut icon" href="/images/zc-icon.jpeg"><link rel="canonical" href="https://flyingzc.github.io/articles/mysql/MySQL%E9%A9%B1%E5%8A%A8add-batch%E4%BC%98%E5%8C%96%E5%AE%9E%E7%8E%B0.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta name="google-site-verification" content="DentpX3LnP_WNgEeFnNIcTZZky-Bby-zL-uI_9GqTAw"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css"><script>
    (() => {
      
    const saveToLocal = {
      set: (key, value, ttl) => {
        if (!ttl) return
        const expiry = Date.now() + ttl * 86400000
        localStorage.setItem(key, JSON.stringify({ value, expiry }))
      },
      get: key => {
        const itemStr = localStorage.getItem(key)
        if (!itemStr) return undefined
        const { value, expiry } = JSON.parse(itemStr)
        if (Date.now() > expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return value
      }
    }

    window.btf = {
      saveToLocal,
      getScript: (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        Object.entries(attr).forEach(([key, val]) => script.setAttribute(key, val))
        script.onload = script.onreadystatechange = () => {
          if (!script.readyState || /loaded|complete/.test(script.readyState)) resolve()
        }
        script.onerror = reject
        document.head.appendChild(script)
      }),
      getCSS: (url, id) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onload = link.onreadystatechange = () => {
          if (!link.readyState || /loaded|complete/.test(link.readyState)) resolve()
        }
        link.onerror = reject
        document.head.appendChild(link)
      }),
      addGlobalFn: (key, fn, name = false, parent = window) => {
        if (!false && key.startsWith('pjax')) return
        const globalFn = parent.globalFn || {}
        globalFn[key] = globalFn[key] || {}
        globalFn[key][name || Object.keys(globalFn[key]).length] = fn
        parent.globalFn = globalFn
      }
    }
  
      
      const activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      const activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }

      btf.activateDarkMode = activateDarkMode
      btf.activateLightMode = activateLightMode

      const theme = saveToLocal.get('theme')
    
          theme === 'dark' ? activateDarkMode() : theme === 'light' ? activateLightMode() : null
        
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        document.documentElement.classList.toggle('hide-aside', asideStatus === 'hide')
      }
    
      
    const detectApple = () => {
      if (/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)) {
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
  
    })()
  </script><script async="async" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><script>(adsbygoogle = window.adsbygoogle || []).push({
  google_ad_client: '',
  enable_page_level_ads: 'true'
});</script><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false,"highlightFullpage":false,"highlightMacStyle":false},
  copy: {
    success: '复制成功',
    error: '复制失败',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'null',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyloadPlugin: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'MySQL 驱动 Add Batch 优化实现',
  isHighlightShrink: false,
  isToc: true,
  pageType: 'post'
}</script><link rel="stylesheet" href="/self/github-dark.css"><meta name="generator" content="Hexo 7.0.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img text-center"><img src="/images/zc-icon.jpeg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data text-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">84</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">36</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">29</div></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header"><nav id="nav"><span id="blog-info"><a class="nav-site-title" href="/"><span class="site-name">flyingzc's blog</span></a><a class="nav-page-title" href="/"><span class="site-name">MySQL 驱动 Add Batch 优化实现</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><span class="site-page"><i class="fas fa-bars fa-fw"></i></span></div></div></nav><div id="post-info"><h1 class="post-title">MySQL 驱动 Add Batch 优化实现</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2024-03-25T16:00:00.000Z" title="发表于 2024-03-26 00:00:00">2024-03-26</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2025-01-21T01:51:41.212Z" title="更新于 2025-01-21 09:51:41">2025-01-21</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/MySQL/">MySQL</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title=""><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">浏览量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="container post-content" id="article-container"><h1 id="MySQL-驱动-Add-Batch-优化实现"><a href="#MySQL-驱动-Add-Batch-优化实现" class="headerlink" title="MySQL 驱动 Add Batch 优化实现"></a>MySQL 驱动 Add Batch 优化实现</h1><p>MySQL 驱动会在 JDBC URL 添加 rewriteBatchedStatements 参数时，对 batch 操作进行优化。本文测试各种参数组合的行为，并结合驱动代码简单分析。</p>
<h2 id="batch参数组合行为"><a href="#batch参数组合行为" class="headerlink" title="batch参数组合行为"></a>batch参数组合行为</h2><h3 id="useServerPrepStmts-参数"><a href="#useServerPrepStmts-参数" class="headerlink" title="useServerPrepStmts 参数"></a>useServerPrepStmts 参数</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">PreparedStatement</span> <span class="hljs-variable">psmt</span> <span class="hljs-operator">=</span> connection.prepareStatement(<span class="hljs-string">&quot;DELETE FROM t_order WHERE  `order_id` = ?&quot;</span>);<br>psmt.setObject(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>);<br>psmt.execute();<br></code></pre></td></tr></table></figure>

<p>开启:<br>使用服务端预编译，先发送 prepared 语句，再发送 excute 语句</p>
<p>不开启:<br>mysql 驱动会将占位符填充后，明文下发sql.</p>
<p>比如 DELETE FROM t_order WHERE <code>order_id</code> &#x3D; ?; 语句</p>
<p>MySQL 驱动会下发 DELETE FROM t_order WHERE <code>order_id</code> &#x3D; 1;</p>
<h3 id="allowMultiQueries-参数"><a href="#allowMultiQueries-参数" class="headerlink" title="allowMultiQueries 参数"></a>allowMultiQueries 参数</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">Statement</span> <span class="hljs-variable">statement</span> <span class="hljs-operator">=</span> connection.createStatement();<br>statement.execute(<span class="hljs-string">&quot;DELETE FROM t_order WHERE `order_id` = 1;DELETE FROM t_order WHERE `order_id` = 2;&quot;</span>);<br></code></pre></td></tr></table></figure>

<p>不开启:<br>服务端不支持 DELETE FROM t_order WHERE <code>order_id</code> &#x3D; 1;DELETE FROM t_order WHERE <code>order_id</code> &#x3D; 2; 这样的批量delete语句</p>
<p>开启:<br>支持多语句，比如： DELETE FROM t_order WHERE <code>order_id</code> &#x3D; 1;DELETE FROM t_order WHERE <code>order_id</code> &#x3D; 2;</p>
<h3 id="rewriteBatchedStatements-参数"><a href="#rewriteBatchedStatements-参数" class="headerlink" title="rewriteBatchedStatements 参数"></a>rewriteBatchedStatements 参数</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">PreparedStatement</span> <span class="hljs-variable">psmt</span> <span class="hljs-operator">=</span> connection.prepareStatement(<span class="hljs-string">&quot;DELETE FROM t_order WHERE  `order_id` = ?&quot;</span>);<br><span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>; i &lt;= <span class="hljs-number">500</span>; i++) &#123;<br>    psmt.setObject(<span class="hljs-number">1</span>, i);<br>    psmt.addBatch();<br>&#125;<br>psmt.executeBatch();<br></code></pre></td></tr></table></figure>

<p>不开启:<br>batch 操作，在 addBatch 时一条条下发参数值。</p>
<p>开启:<br>在执行 executeBatch 时,将 batch 操作改写后批量下发；改写后的 SQL 比如 DELETE FROM t_order WHERE <code>order_id</code> &#x3D; 1;DELETE FROM t_order WHERE <code>order_id</code> &#x3D; 2;…;DELETE FROM t_order WHERE <code>order_id</code> &#x3D; 500;</p>
<h3 id="useServerPrepStmts-allowMultiQueries-参数"><a href="#useServerPrepStmts-allowMultiQueries-参数" class="headerlink" title="useServerPrepStmts+allowMultiQueries 参数"></a>useServerPrepStmts+allowMultiQueries 参数</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">PreparedStatement</span> <span class="hljs-variable">psmt</span> <span class="hljs-operator">=</span> connection.prepareStatement(<span class="hljs-string">&quot;DELETE FROM t_order WHERE  `order_id` = ?;DELETE FROM t_order WHERE  `order_id` = ?;&quot;</span>);<br>psmt.execute();<br></code></pre></td></tr></table></figure>

<p>虽然开启了服务端预编译参数 useServerPrepStmts，但是 MySQL JDBC 驱动会判断预编译 SQL 不支持 allowMultiQueries，会直接转换成客户端预编译，也就时会将占位符赋值后下发到 mysql。</p>
<p>比如对于以下 SQL: DELETE FROM t_order WHERE <code>order_id</code> &#x3D; ?;DELETE FROM t_order WHERE <code>order_id</code> &#x3D; ?;</p>
<p>客户端会将占位符填充后发送: DELETE FROM t_order WHERE <code>order_id</code> &#x3D; 1;DELETE FROM t_order WHERE <code>order_id</code> &#x3D; 2;</p>
<h3 id="useServerPrepStmts-rewriteBatchedStatements-参数"><a href="#useServerPrepStmts-rewriteBatchedStatements-参数" class="headerlink" title="useServerPrepStmts+rewriteBatchedStatements 参数"></a>useServerPrepStmts+rewriteBatchedStatements 参数</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">PreparedStatement</span> <span class="hljs-variable">psmt</span> <span class="hljs-operator">=</span> connection.prepareStatement(<span class="hljs-string">&quot;DELETE FROM t_order WHERE  `order_id` = ?&quot;</span>);<br><span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>; i &lt; <span class="hljs-number">500</span>; i++) &#123;<br>    psmt.setObject(<span class="hljs-number">1</span>, i);<br>    psmt.addBatch();<br>&#125;<br>psmt.executeBatch();<br></code></pre></td></tr></table></figure>

<p>对于 DELETE FROM t_order WHERE <code>order_id</code> &#x3D; ? 的 batch 语句，</p>
<p>会转换成 DELETE FROM t_order WHERE <code>order_id</code> &#x3D; ?;DELETE FROM t_order WHERE <code>order_id</code> &#x3D; ?;多语句下发，但是服务端返回不支持。</p>
<p>然后客户端再使用客户端预编译尝试发送(会先发送 set multi option on 包,执行完再关闭该标识)，然后下发多语句 DELETE FROM t_order WHERE <code>order_id</code> &#x3D; 1;DELETE FROM t_order WHERE <code>order_id</code> &#x3D; 2;…;DELETE FROM t_order WHERE <code>order_id</code> &#x3D; 500;</p>
<h3 id="useServerPrepStmts-allowMultiQueries-rewriteBatchedStatements-参数"><a href="#useServerPrepStmts-allowMultiQueries-rewriteBatchedStatements-参数" class="headerlink" title="useServerPrepStmts+allowMultiQueries+rewriteBatchedStatements 参数"></a>useServerPrepStmts+allowMultiQueries+rewriteBatchedStatements 参数</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">PreparedStatement</span> <span class="hljs-variable">psmt</span> <span class="hljs-operator">=</span> connection.prepareStatement(<span class="hljs-string">&quot;DELETE FROM t_order WHERE  `order_id` = ?&quot;</span>);<br><span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>; i &lt; <span class="hljs-number">500</span>; i++) &#123;<br>    psmt.setObject(<span class="hljs-number">1</span>, i);<br>    psmt.addBatch();<br>&#125;<br>psmt.executeBatch();<br></code></pre></td></tr></table></figure>

<p>当执行 batch delete 语句时：</p>
<p>虽然开启了 useServerPrepStmts 预编译参数，但是 MySQL JDBC 驱动会判断预编译 SQL 不支持 allowMultiQueries，会直接转换成客户端预编译，也就将占位符赋值后下发到服务端。</p>
<p>也就是发送 DELETE FROM t_order WHERE <code>order_id</code> &#x3D; 1;DELETE FROM t_order WHERE <code>order_id</code> &#x3D; 2;…;DELETE FROM t_order WHERE <code>order_id</code> &#x3D; 500;</p>
<h1 id="MySQL-驱动代码分析"><a href="#MySQL-驱动代码分析" class="headerlink" title="MySQL 驱动代码分析"></a>MySQL 驱动代码分析</h1><p>下面结合驱动代码简单分析两组参数的流程:</p>
<h2 id="1-useServerPrepStmts-rewriteBatchedStatements-allowMultiQueries-参数"><a href="#1-useServerPrepStmts-rewriteBatchedStatements-allowMultiQueries-参数" class="headerlink" title="1.useServerPrepStmts+rewriteBatchedStatements+allowMultiQueries 参数"></a>1.useServerPrepStmts+rewriteBatchedStatements+allowMultiQueries 参数</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 测试 demo</span><br><span class="hljs-comment">// jdbc:mysql://localhost:3306/test?serverTimezone=UTC&amp;useSSL=false&amp;allowPublicKeyRetrieval=true&amp;useServerPrepStmts=true&amp;cachePrepStmts=true&amp;prepStmtCacheSize=512&amp;prepStmtCacheSqlLimit=8000&amp;rewriteBatchedStatements=true&amp;allowMultiQueries=true</span><br><span class="hljs-type">PreparedStatement</span> <span class="hljs-variable">psmt</span> <span class="hljs-operator">=</span> connection.prepareStatement(<span class="hljs-string">&quot;DELETE FROM t_order WHERE  `order_id` = ?&quot;</span>);<br><span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>; i &lt; <span class="hljs-number">500</span>; i++) &#123;<br>    psmt.setObject(<span class="hljs-number">1</span>, i);<br>    psmt.addBatch();<br>&#125;<br>psmt.executeBatch();<br></code></pre></td></tr></table></figure>

<p>MySQL驱动处理逻辑：</p>
<h3 id="1-prepareStatement"><a href="#1-prepareStatement" class="headerlink" title="1.prepareStatement"></a>1.prepareStatement</h3><p>当执行 prepareStatement() 时，因为开启了 useServerPrepStmts 参数，所以会下发预编译 sql 给 server 端。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs Java"><span class="hljs-type">PreparedStatement</span> <span class="hljs-variable">psmt</span> <span class="hljs-operator">=</span> connection.prepareStatement(sql);<br></code></pre></td></tr></table></figure>

<p>相关调用栈如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs Java">com.mysql.cj.jdbc.ConnectionImpl#prepareStatement<br>    com.mysql.cj.jdbc.ConnectionImpl#canHandleAsServerPreparedStatement<br>    <span class="hljs-number">1.</span>判断当前sql是否支持服务端预编译<br>    com.mysql.cj.jdbc.ServerPreparedStatement#getInstance<br>        com.mysql.cj.jdbc.ServerPreparedStatement#serverPrepare<br>        <span class="hljs-number">2.</span>下发预编译sql<br>            com.mysql.cj.ServerPreparedQuery#serverPrepare<br>                com.mysql.cj.protocol.a.NativeMessageBuilder#buildComStmtPrepare<br>                com.mysql.cj.NativeSession#sendCommand<br>                发包<br>                com.mysql.cj.protocol.a.NativeProtocol#read<br>                    com.mysql.cj.protocol.a.ColumnDefinitionReader#unpackField<br></code></pre></td></tr></table></figure>

<p>1.开启 useServerPrepStmts 参数(emulateUnsupportedPstmts 参数默认就为true)，会再根据 canHandleAsServerPreparedStatement() 判断当前 sql 是否支持服务端预编译。</p>
<p><img src="/images/img-20240325200018-1.jpg"></p>
<p>2.canHandleAsServerPreparedStatement()方法会通过 <code>StringUtils._canHandleAsServerPreparedStatementNoCache()</code> 方法检测 sql 是否可以支持服务端预编译。</p>
<p><img src="/images/img-20240325200019-2.jpg"></p>
<ol start="3">
<li><code>com.mysql.cj.jdbc.ServerPreparedStatement#getInstance</code> 发送预编译 sql</li>
</ol>
<p><img src="/images/img-20240325200019-3.jpg"></p>
<h3 id="2-addBatch"><a href="#2-addBatch" class="headerlink" title="2.addBatch"></a>2.addBatch</h3><p>添加参数即可，不和服务端交互。</p>
<h3 id="3-executeBatch"><a href="#3-executeBatch" class="headerlink" title="3.executeBatch"></a>3.executeBatch</h3><p>调用栈:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs plaintext">com.mysql.cj.jdbc.StatementImpl#executeBatch<br>    com.mysql.cj.jdbc.ClientPreparedStatement#executeBatchInternal<br>        com.mysql.cj.jdbc.StatementImpl#statementBegins<br>            com.mysql.cj.ServerPreparedQuery#statementBegins<br>                com.mysql.cj.AbstractQuery#statementBegins<br>        com.mysql.cj.AbstractPreparedQuery#getParseInfo<br>        com.mysql.cj.ParseInfo#canRewriteAsMultiValueInsertAtSqlLevel 是否支持insert values改写优化<br>        com.mysql.cj.jdbc.ClientPreparedStatement#executePreparedBatchAsMultiStatement 多语句执行优化<br>            com.mysql.cj.AbstractQuery#getBatchedArgs<br>            com.mysql.cj.AbstractPreparedQuery#computeBatchSize 计算批次大小<br>                com.mysql.cj.ServerPreparedQuery#computeMaxParameterSetSizeAndBatchSize<br>            com.mysql.cj.jdbc.ClientPreparedStatement#generateMultiStatementForBatch<br>            com.mysql.cj.jdbc.ConnectionImpl#prepareStatement<br>                com.mysql.cj.jdbc.ConnectionImpl#prepareStatement<br>            com.mysql.cj.jdbc.ServerPreparedStatement#setOneBatchedParameterSet<br>            com.mysql.cj.jdbc.ClientPreparedStatement#execute<br>                com.mysql.cj.jdbc.ClientPreparedStatement#checkReadOnlySafeStatement<br>                    com.mysql.cj.protocol.a.NativeMessageBuilder#buildComQuery<br>                    com.mysql.cj.NativeSession#sendCommand<br></code></pre></td></tr></table></figure>

<ol>
<li>由于开启了 rewriteBatchedStatements 参数，并且 delelte batch size 大于 3,会通过 executePreparedBatchAsMultiStatement 优化成 批量 delete.</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs Java"><span class="hljs-comment">// 开启 rewriteBatchedStatements 参数</span><br><span class="hljs-keyword">if</span> (!<span class="hljs-built_in">this</span>.batchHasPlainStatements &amp;&amp; <span class="hljs-built_in">this</span>.rewriteBatchedStatements.getValue()) &#123;<br>    <span class="hljs-comment">// batch size &gt; 3,则将 sql 转成 multi delete</span><br>    <span class="hljs-keyword">if</span> (!<span class="hljs-built_in">this</span>.batchHasPlainStatements &amp;&amp; <span class="hljs-built_in">this</span>.query.getBatchedArgs() != <span class="hljs-literal">null</span><br>            &amp;&amp; <span class="hljs-built_in">this</span>.query.getBatchedArgs().size() &gt; <span class="hljs-number">3</span> <span class="hljs-comment">/* cost of option setting rt-wise */</span>) &#123;<br>        <span class="hljs-keyword">return</span> executePreparedBatchAsMultiStatement(batchTimeout);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<ol start="2">
<li>executePreparedBatchAsMultiStatement 方法</li>
</ol>
<p>计算需要分几个批次下发。正常500条sql一个批次即可。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs Java"><span class="hljs-comment">// 根据上面得到的batch sql的长度，确定分几个批次下发 multi sql;</span><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * Computes the optimum number of batched parameter lists to send</span><br><span class="hljs-comment"> * without overflowing max_allowed_packet.</span><br><span class="hljs-comment"> * </span><br><span class="hljs-comment"> * <span class="hljs-doctag">@param</span> numBatchedArgs</span><br><span class="hljs-comment"> *            original batch size</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@return</span> computed batch size</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">computeBatchSize</span><span class="hljs-params">(<span class="hljs-type">int</span> numBatchedArgs)</span> &#123;<br>    <span class="hljs-type">long</span>[] combinedValues = computeMaxParameterSetSizeAndBatchSize(numBatchedArgs);<br><br>    <span class="hljs-type">long</span> <span class="hljs-variable">maxSizeOfParameterSet</span> <span class="hljs-operator">=</span> combinedValues[<span class="hljs-number">0</span>];<br>    <span class="hljs-type">long</span> <span class="hljs-variable">sizeOfEntireBatch</span> <span class="hljs-operator">=</span> combinedValues[<span class="hljs-number">1</span>];<br><br>    <span class="hljs-comment">// 整个 batch sql 的长度不能超长，正常走到这个分支里</span><br>    <span class="hljs-keyword">if</span> (sizeOfEntireBatch &lt; <span class="hljs-built_in">this</span>.maxAllowedPacket.getValue() - <span class="hljs-built_in">this</span>.originalSql.length()) &#123;<br>        <span class="hljs-keyword">return</span> numBatchedArgs;<br>    &#125;<br><br>    <span class="hljs-keyword">return</span> (<span class="hljs-type">int</span>) Math.max(<span class="hljs-number">1</span>, (<span class="hljs-built_in">this</span>.maxAllowedPacket.getValue() - <span class="hljs-built_in">this</span>.originalSql.length()) / maxSizeOfParameterSet);<br>&#125;<br></code></pre></td></tr></table></figure>

<p>计算方法如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><code class="hljs Java"><span class="hljs-comment">// 计算 maxSizeOfParameterSet: 每个参数中最大的长度</span><br><span class="hljs-comment">// sizeOfEntireBatch: batch所有参数长度相加</span><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * Computes the maximum parameter set size and the size of the entire batch given</span><br><span class="hljs-comment"> * the number of arguments in the batch.</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Override</span><br><span class="hljs-keyword">protected</span> <span class="hljs-type">long</span>[] computeMaxParameterSetSizeAndBatchSize(<span class="hljs-type">int</span> numBatchedArgs) &#123;<br><br>    <span class="hljs-type">long</span> <span class="hljs-variable">sizeOfEntireBatch</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span> + <span class="hljs-comment">/* com_execute */</span>+<span class="hljs-number">4</span> <span class="hljs-comment">/* stmt id */</span> + <span class="hljs-number">1</span> <span class="hljs-comment">/* flags */</span> + <span class="hljs-number">4</span> <span class="hljs-comment">/* batch count padding */</span>;<br>    <span class="hljs-type">long</span> <span class="hljs-variable">maxSizeOfParameterSet</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br><br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; numBatchedArgs; i++) &#123;<br>        ServerPreparedQueryBindValue[] paramArg = ((ServerPreparedQueryBindings) <span class="hljs-built_in">this</span>.batchedArgs.get(i)).getBindValues();<br><br>        <span class="hljs-type">long</span> <span class="hljs-variable">sizeOfParameterSet</span> <span class="hljs-operator">=</span> (<span class="hljs-built_in">this</span>.parameterCount + <span class="hljs-number">7</span>) / <span class="hljs-number">8</span>; <span class="hljs-comment">// for isNull</span><br>        sizeOfParameterSet += <span class="hljs-built_in">this</span>.parameterCount * <span class="hljs-number">2</span>; <span class="hljs-comment">// have to send types</span><br><br>        ServerPreparedQueryBindValue[] parameterBindings = <span class="hljs-built_in">this</span>.queryBindings.getBindValues();<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">j</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; j &lt; parameterBindings.length; j++) &#123;<br>            <span class="hljs-keyword">if</span> (!paramArg[j].isNull()) &#123;<br><br>                <span class="hljs-type">long</span> <span class="hljs-variable">size</span> <span class="hljs-operator">=</span> paramArg[j].getBoundLength();<br><br>                <span class="hljs-keyword">if</span> (paramArg[j].isStream()) &#123;<br>                    <span class="hljs-keyword">if</span> (size != -<span class="hljs-number">1</span>) &#123;<br>                        sizeOfParameterSet += size;<br>                    &#125;<br>                &#125; <span class="hljs-keyword">else</span> &#123;<br>                    sizeOfParameterSet += size;<br>                &#125;<br>            &#125;<br>        &#125;<br><br>        sizeOfEntireBatch += sizeOfParameterSet;<br><br>        <span class="hljs-keyword">if</span> (sizeOfParameterSet &gt; maxSizeOfParameterSet) &#123;<br>            maxSizeOfParameterSet = sizeOfParameterSet;<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">long</span>[] &#123; maxSizeOfParameterSet, sizeOfEntireBatch &#125;;<br>&#125;<br></code></pre></td></tr></table></figure>

<ol start="3">
<li>拼接预编译sql,此时还有?号占位符</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs Java">((Wrapper) locallyScopedConn.prepareStatement(generateMultiStatementForBatch(numValuesPerBatch)))<br>        .unwrap(java.sql.PreparedStatement.class);<br></code></pre></td></tr></table></figure>

<p><img src="/images/img-20240325200019-4.jpg"></p>
<p>得到sql如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs Java">DELETE FROM t_order WHERE  `order_id` = ?;DELETE FROM t_order WHERE  `order_id` = ?;......<br></code></pre></td></tr></table></figure>

<ol start="4">
<li>执行 prepareStatement，将上面的批量delete语句执行预编译。</li>
</ol>
<p>这里逻辑和第一步里的 prepareStatement 类似，也需要判断当前批量delete sql是否支持服务端预编译。</p>
<p>这里可以看到，开启 allowMultiQueries 参数之后，驱动会查找 sql 里是否包含 <code>;</code> 号，如果包含，不支持服务端预编译。后面会走客户端预编译流程。</p>
<p><img src="/images/img-20240325200020-5.jpg"></p>
<ol start="5">
<li>客户端预编译执行批量delete</li>
</ol>
<p><img src="/images/img-20240325200020-6.jpg"></p>
<ol start="6">
<li>设置参数</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs Java"><span class="hljs-comment">// 给?号占位符塞值</span><br>batchedParamIndex = setOneBatchedParameterSet(batchedStatement, batchedParamIndex, <span class="hljs-built_in">this</span>.query.getBatchedArgs().get(batchCounter++));<br></code></pre></td></tr></table></figure>

<ol start="7">
<li>执行批量delete语句</li>
</ol>
<p><img src="/images/img-20240325200020-7.jpg"></p>
<p><img src="/images/img-20240325200020-8.jpg"></p>
<h2 id="2-useServerPrepStmts-rewriteBatchedStatements-参数"><a href="#2-useServerPrepStmts-rewriteBatchedStatements-参数" class="headerlink" title="2.useServerPrepStmts+rewriteBatchedStatements 参数"></a>2.useServerPrepStmts+rewriteBatchedStatements 参数</h2><p>1.下发预编译sql</p>
<p><img src="/images/img-20240325200021-9.jpg"></p>
<p>2.设置 multi statement on</p>
<p><img src="/images/img-20240325200021-10.jpg"></p>
<p>3.多语句预编译sql,执行返回失败</p>
<p><img src="/images/img-20240325200021-11.jpg"></p>
<p>You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘DELETE FROM t_order WHERE <code>order_id</code> &#x3D; ?;DELETE FROM t_order WHERE <code>order_id</code> ‘ at line 1</p>
<p>代码就是服务端prepared返回失败，会再用 client Prepare statement 重试</p>
<p><img src="/images/img-20240325200022-12.jpg"></p>
<p>4.client Prepare statement 重新批量delete 语句</p>
<p><img src="/images/img-20240325200022-13.jpg"></p>
<link rel="stylesheet" href="https://fastly.jsdelivr.net/npm/markmap-toolbar@0.18.10/dist/style.css"><script src="https://fastly.jsdelivr.net/npm/d3@7"></script><script src="https://fastly.jsdelivr.net/npm/markmap-view@0.18.10"></script><script src="https://fastly.jsdelivr.net/npm/markmap-toolbar@0.18.10"></script>
<link rel="stylesheet" href="/css/markmap.css">

<script src="/js/markmap.js"></script>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://flyingzc.github.io">flyingzc</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://flyingzc.github.io/articles/mysql/MySQL%E9%A9%B1%E5%8A%A8add-batch%E4%BC%98%E5%8C%96%E5%AE%9E%E7%8E%B0.html">https://flyingzc.github.io/articles/mysql/MySQL%E9%A9%B1%E5%8A%A8add-batch%E4%BC%98%E5%8C%96%E5%AE%9E%E7%8E%B0.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来源 <a href="https://flyingzc.github.io" target="_blank">flyingzc's blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/MySQL/">MySQL</a></div><div class="post-share"><div class="social-share" data-image="/images/zc-icon.jpeg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info text-center"><div class="avatar-img"><img src="/images/zc-icon.jpeg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info-name">flyingzc</div><div class="author-info-description">不积跬步无以至千里<br/>不积小流无以成江海</div><div class="site-data"><a href="/archives/"><div class="headline">文章</div><div class="length-num">84</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">36</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">29</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/FlyingZC"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons"><a class="social-icon" href="https://github.com/FlyingZC" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">FlyingZC's Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#MySQL-%E9%A9%B1%E5%8A%A8-Add-Batch-%E4%BC%98%E5%8C%96%E5%AE%9E%E7%8E%B0"><span class="toc-number">1.</span> <span class="toc-text">MySQL 驱动 Add Batch 优化实现</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#batch%E5%8F%82%E6%95%B0%E7%BB%84%E5%90%88%E8%A1%8C%E4%B8%BA"><span class="toc-number">1.1.</span> <span class="toc-text">batch参数组合行为</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#useServerPrepStmts-%E5%8F%82%E6%95%B0"><span class="toc-number">1.1.1.</span> <span class="toc-text">useServerPrepStmts 参数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#allowMultiQueries-%E5%8F%82%E6%95%B0"><span class="toc-number">1.1.2.</span> <span class="toc-text">allowMultiQueries 参数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#rewriteBatchedStatements-%E5%8F%82%E6%95%B0"><span class="toc-number">1.1.3.</span> <span class="toc-text">rewriteBatchedStatements 参数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#useServerPrepStmts-allowMultiQueries-%E5%8F%82%E6%95%B0"><span class="toc-number">1.1.4.</span> <span class="toc-text">useServerPrepStmts+allowMultiQueries 参数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#useServerPrepStmts-rewriteBatchedStatements-%E5%8F%82%E6%95%B0"><span class="toc-number">1.1.5.</span> <span class="toc-text">useServerPrepStmts+rewriteBatchedStatements 参数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#useServerPrepStmts-allowMultiQueries-rewriteBatchedStatements-%E5%8F%82%E6%95%B0"><span class="toc-number">1.1.6.</span> <span class="toc-text">useServerPrepStmts+allowMultiQueries+rewriteBatchedStatements 参数</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#MySQL-%E9%A9%B1%E5%8A%A8%E4%BB%A3%E7%A0%81%E5%88%86%E6%9E%90"><span class="toc-number">2.</span> <span class="toc-text">MySQL 驱动代码分析</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-useServerPrepStmts-rewriteBatchedStatements-allowMultiQueries-%E5%8F%82%E6%95%B0"><span class="toc-number">2.1.</span> <span class="toc-text">1.useServerPrepStmts+rewriteBatchedStatements+allowMultiQueries 参数</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-prepareStatement"><span class="toc-number">2.1.1.</span> <span class="toc-text">1.prepareStatement</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-addBatch"><span class="toc-number">2.1.2.</span> <span class="toc-text">2.addBatch</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-executeBatch"><span class="toc-number">2.1.3.</span> <span class="toc-text">3.executeBatch</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-useServerPrepStmts-rewriteBatchedStatements-%E5%8F%82%E6%95%B0"><span class="toc-number">2.2.</span> <span class="toc-text">2.useServerPrepStmts+rewriteBatchedStatements 参数</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/ignite/ignite03-transaction-begin.html" title="Apache Ignite 开启事务执行流程">Apache Ignite 开启事务执行流程</a><time datetime="2025-10-10T16:00:00.000Z" title="发表于 2025-10-11 00:00:00">2025-10-11</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/ignite/ignite02-transaction.html" title="Apache Ignite 分布式事务原理">Apache Ignite 分布式事务原理</a><time datetime="2025-09-17T16:00:00.000Z" title="发表于 2025-09-18 00:00:00">2025-09-18</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/ignite/ignite01-build.html" title="Apache Ignite IDEA 环境搭建">Apache Ignite IDEA 环境搭建</a><time datetime="2025-09-10T16:00:00.000Z" title="发表于 2025-09-11 00:00:00">2025-09-11</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/druid/druid01-resultset-leak.html" title="ShardingSphere JDBC 使用 Druid 连接池 ResultSet 内存泄露分析">ShardingSphere JDBC 使用 Druid 连接池 ResultSet 内存泄露分析</a><time datetime="2025-06-30T16:00:00.000Z" title="发表于 2025-07-01 00:00:00">2025-07-01</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/jmeter/jmeter01-shardingsphere.html" title="Jmeter 压测 ShardingSphere">Jmeter 压测 ShardingSphere</a><time datetime="2025-06-30T16:00:00.000Z" title="发表于 2025-07-01 00:00:00">2025-07-01</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2025 By flyingzc</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="日间和夜间模式切换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>